/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
*
*
* Authors:                  zhoujian
* Email:            343600434@qq.com
* Date:         Aug-30-2024 11:00:48
*
*/


#ifndef __jsm1302z_H__
#define __jsm1302z_H__

#include <stdint.h>


#ifdef __cplusplus
extern "C" {
#endif

#define RST_ON do{GPIO_BOP(GPIOB) = (uint32_t)GPIO_PIN_13;}while(0)
#define RST_OFF do{GPIO_BC(GPIOB) = (uint32_t)GPIO_PIN_13;}while(0)

#define IS_I2C_SDA_H (GPIO_ISTAT(GPIOB) & (GPIO_PIN_12))

#define I2C_SDA_INPUT do{gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_12);}while(0)
#define I2C_SDA_OUTPUT do{/*rcu_periph_clock_enable(RCU_GPIOC);*/gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_12);}while(0)

#define I2C_SDA_H do{GPIO_BOP(GPIOB) = (uint32_t)GPIO_PIN_12;}while(0)


//#define I2C_D_L gpio_bit_reset(GPIOC, GPIO_PIN_7)
#define I2C_SDA_L do{GPIO_BC(GPIOB) = (uint32_t)GPIO_PIN_12;}while(0)
//#define I2C_C_H gpio_bit_set(GPIOC, GPIO_PIN_6)
#define I2C_SCL_H  do{GPIO_BOP(GPIOB) = (uint32_t)GPIO_PIN_11;}while(0)
//#define I2C_C_L gpio_bit_reset(GPIOC, GPIO_PIN_6)
#define I2C_SCL_L do{GPIO_BC(GPIOB) = (uint32_t)GPIO_PIN_11;}while(0)


typedef struct CALENDAR {
    uint8_t year;
    uint8_t mon;
    uint8_t mday;
    uint8_t wday;
    uint8_t hour;
    uint8_t min;
    uint8_t sec;
} CALENDAR;

typedef enum {
    JSM1302Z_State_0x00 = 0x00,
    JSM1302Z_State_0x01 = 0x01,
    JSM1302Z_State_0x02 = 0x02,
    JSM1302Z_State_0x03 = 0x03,
} JSM1302Z_zTypeDef;


extern JSM1302Z_zTypeDef jsm1302z_state;


void jsm1302z_init(void);
void jsm1302z_task(void);
void jsm1302z_deinit(void);
void set_delay_lock(uint32_t lock_time);


uint8_t read_jsm1302z(uint8_t cmd);
uint8_t write_jsm1302z(uint8_t cmd, uint8_t dat);
void jsm1302z_read_time(void);
void TimeStampToTime(uint32_t TimeStamp);

extern volatile uint8_t time_end;
extern volatile uint8_t show_time_update;
extern volatile uint8_t no_rtc;
extern CALENDAR calendar;



#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
class jsm1302z
{

public:
    jsm1302z();
    ~jsm1302z();

};
#endif



#endif
